package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.goldwind.iot.board.utils.mybatisUtil.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;


/**
* <p>
* ${table.comment!} 服务实现类
* </p>
*
* @author ${author}
* @since ${date}
*/
@Service("${table.serviceName}")
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {

    private static final Logger log = LoggerFactory.getLogger(${table.serviceImplName}.class);

    @Autowired
    private PageUtils<${entity}> pageUtils;


    /**
    * 获取分页列表
    * @param param 根据需要进行传值
    * @return
    */
    @Override
    public IPage<${entity}> pageList(${entity} param) {
        //构建查询条件
        LambdaQueryWrapper<${entity}> queryWrapper = this.buildQuery(param);
        //分页查询数据
        IPage<${entity}> page = this.page(pageUtils.page(), queryWrapper);
        return page;
    }

    /**
    * 查询列表
    * @param param 根据需要进行传值
    * @return
    */
    @Override
    public List<${entity}> queryList(${entity} param) {
        //构建查询条件
        LambdaQueryWrapper<${entity}> queryWrapper = this.buildQuery(param);
        //条件查询数据
        List<${entity}> returnData = this.list(queryWrapper);
        return returnData;
    }

    /**
    * 获取详情
    * @param id
    * @return
    */
    @Override
    public ${entity} info(Long id) {
        return this.getById(id);
    }

    /**
    * 新增
    * @param param 根据需要进行传值
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(${entity} param) {
        log.info("${table.comment!}-新增，参数：{}", param);
        return this.save(param);
    }

    /**
    * 修改
    * @param param 根据需要进行传值
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean edit(${entity} param) {
        log.info("${table.comment!}-修改，参数：{}", param);
        return this.updateById(param);
    }

    /**
    * ${table.comment!}删除(多个条目)
    * @param ids
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchDelete(List<Long> ids) {
        log.info("${table.comment!}-批量删除，参数：{}", ids);
        return this.removeByIds(ids);
    }

    // =================================================================================================================
    // =================================================== ⬇⬇⬇私有化方法⬇⬇⬇ ===============================================
    // =================================================================================================================

    /**
    * 公共构建查询条件
    * @param param 根据需要进行传值
    * @return
    */
    private LambdaQueryWrapper<${entity}> buildQuery(${entity} param) {
        LambdaQueryWrapper<${entity}> wq = Wrappers.<${entity}>lambdaQuery()
        <#list table.fields as field>
        // ${field.comment}
        <#if !entityLombokModel>
            <#if field.propertyType == "Boolean">
                <#assign getprefix="is"/>
            <#else>
                <#assign getprefix="get"/>
            </#if>
            <#if field.propertyType == "String">
                .eq(!StringUtils.isBlank(param.${getprefix}${field.capitalName}()), ${entity}::${getprefix}${field.capitalName}, param.${getprefix}${field.capitalName}())
            <#else>
                .eq(param.${getprefix}${field.capitalName}() != null, ${entity}::${getprefix}${field.capitalName}, param.${getprefix}${field.capitalName}())
            </#if>
        <#else>
            <#if field.propertyType == "String">
                .eq(!StringUtils.isBlank(param.get${field.capitalName}()), ${entity}::get${field.capitalName}, param.get${field.capitalName}())
            <#else>
                .eq(param.get${field.capitalName}() != null, ${entity}::get${field.capitalName}, param.get${field.capitalName}())
            </#if>
        </#if>
        </#list>;
        return wq;
    }
}
</#if>
